using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;


namespace TNI
{
    public class ImageTNI : Signal2D<Color>
    {
        private int m_width;
        private int m_heigth;


        public ImageTNI(string path)
        {

            m_data = new List<Color>();
            Bitmap image = (Bitmap)Bitmap.FromFile(path);

            m_width = image.Width;
            m_heigth = image.Height;
            m_nbCol = m_width;
            m_nbLigne = m_heigth;
            m_nbElement = m_nbLigne * m_nbCol;
            for (int y = 0; y < m_heigth; y++)
            {
                for (int x = 0; x < m_width; x++)
                {
                    System.Drawing.Color colorBitmap = image.GetPixel(x, y);
                    Color color = new Color();
                    color.RGBColor = colorBitmap;

                    m_data.Add(color);
                }
            }
        }

        //======================== ACCESSOR ==========================
        public int Weight { get { return m_width; } set { m_width = value; } }
        public int Heigth { get { return m_heigth; } set { m_heigth = value; } }
        //=============================================================

       public System.Drawing.Color GetGrayNaturalPixel(int x, int y)
       {
           int position = (y * m_nbCol) + x;

            double niveauDeGris = m_data[position].NiveauDeGrisNaturel;
            System.Drawing.Color bitmapColor = new System.Drawing.Color();
            if (niveauDeGris > 0)
            {
                bitmapColor = System.Drawing.Color.FromArgb((int)niveauDeGris * 255, (int)niveauDeGris * 255, (int)niveauDeGris * 255);

            }

            return bitmapColor;

       }

        public System.Drawing.Color GetGrayLinearPixel(int x, int y)
        {
            int position = (y * m_nbCol) + x;

            double niveauDeGris = m_data[position].NiveauDeGrisCouleurLineaire;
            System.Drawing.Color bitmapColor = System.Drawing.Color.FromArgb((int)niveauDeGris, (int)niveauDeGris, (int)niveauDeGris);



            return bitmapColor;

        }




        public System.Drawing.Color GetPixel(int x, int y)
        {
            int position = (y * m_nbCol) + x;

            System.Drawing.Color bitmapColor = m_data[position].RGBColor;
            return bitmapColor;

        }



       public Bitmap GetBitmapImage()
       {
           Bitmap image = new Bitmap(m_width, m_heigth);
           for (int y = 0; y < m_width-1; y++)
           {
               for (int x = 0; x < m_width && y < m_heigth; x++)
               {
                   image.SetPixel(x, y, GetPixel(x, y));
               }
           }
           return image;
       }

        public Bitmap GetGrayscaleNaturalImage()
        {
            Bitmap image = new Bitmap(m_width, m_heigth);
            for (int y = 0; y < m_width - 1; y++)
            {
                for (int x = 0; x < m_width && y < m_heigth; x++)
                {
                    image.SetPixel(x, y, GetGrayNaturalPixel(x, y));
                }
            }
            return image;
        }

        public Bitmap GetGrayscaleLinearImage()
        {
            Bitmap image = new Bitmap(m_width, m_heigth);
            for (int y = 0; y < m_width - 1; y++)
            {
                for (int x = 0; x < m_width && y < m_heigth; x++)
                {
                    image.SetPixel(x, y, GetGrayNaturalPixel(x, y));
                }
            }
            return image;
        }

        //List<Data> GetGrayScaleData

    }
}
